#!/usr/bin/env bash
#
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.

# Symlink bin command to /usr/bin
rm -f /usr/bin/@@NAME@@
ln -s /usr/share/@@NAME@@/bin/@@NAME@@ /usr/bin/@@NAME@@

# Register code in the alternatives system
# Priority of 0 should never make code the default editor in auto mode as most
# developers would prefer a terminal editor as the default.
update-alternatives --install /usr/bin/editor editor /usr/bin/@@NAME@@ 0

# Install the desktop entry
if hash update-desktop-database 2>/dev/null; then
	update-desktop-database
fi

# Update mimetype database to pickup workspace mimetype
if hash update-mime-database 2>/dev/null; then
	update-mime-database /usr/share/mime
fi

if [ "@@NAME@@" != "code-oss" ]; then
	# Remove the legacy bin command if this is the stable build
	if [ "@@NAME@@" = "code" ]; then
		rm -f /usr/local/bin/code
	fi

	# Register apt repository
	eval $(apt-config shell APT_SOURCE_PARTS Dir::Etc::sourceparts/d)
	CODE_SOURCE_PART=${APT_SOURCE_PARTS}vscode.list
	CODE_SOURCE_PART_DEB822=${APT_SOURCE_PARTS}vscode.sources

	CODE_TRUSTED_PART=/usr/share/keyrings/microsoft.gpg
	CODE_TRUSTED_PART_OLD="/etc/apt/trusted.gpg.d/microsoft.gpg"

	# RET seems to be true by default even after db_get is called on a first install.
	RET='true'
	if [ -e '/usr/share/debconf/confmodule' ]; then
		. /usr/share/debconf/confmodule
		db_get @@NAME@@/add-microsoft-repo || true
	fi

	# Determine whether to write the Microsoft repository source list
	WRITE_SOURCE='no'
	if [ "$RET" = 'false' ]; then
		# The user specified in debconf not to add the Microsoft repository
		WRITE_SOURCE='no'
	elif [ -f "$CODE_SOURCE_PART" ]; then
		# The user is not on the new DEB822 format
		WRITE_SOURCE='yes'
	elif [ -f "$CODE_SOURCE_PART_DEB822" ]; then
		# The user is on the new DEB822 format, but refresh the file contents
		WRITE_SOURCE='yes'
	elif [ -f /etc/rpi-issue ]; then
		# Do not write on Raspberry Pi OS
		# https://github.com/microsoft/vscode/issues/118825
		WRITE_SOURCE='no'
	else
		WRITE_SOURCE='ask'
	fi

	if [ "$WRITE_SOURCE" = 'ask' ]; then
		if ! [ -t 1 ]; then
			# By default, write sources in a non-interactive terminal
			# to match old behavior.
			WRITE_SOURCE='yes'
		elif [ -e '/usr/share/debconf/confmodule' ]; then
			# Ask the user whether to actually write the source list
			db_input high @@NAME@@/add-microsoft-repo || true
			db_go || true

			db_get @@NAME@@/add-microsoft-repo
			if [ "$RET" = false ]; then
				WRITE_SOURCE='no'
			else
				WRITE_SOURCE='yes'
			fi
		else
			# The terminal is interactive but there is no debconf.
			# Write sources to match old behavior.
			WRITE_SOURCE='yes'
		fi
	fi

	if [ "$WRITE_SOURCE" != 'no' ]; then
		# Write repository in deb822 format with Signed-By.
		echo "### THIS FILE IS AUTOMATICALLY CONFIGURED ###
# You may comment out this entry, but any other modifications may be lost." > "$CODE_SOURCE_PART_DEB822"
		cat <<EOF >> "$CODE_SOURCE_PART_DEB822"
Types: deb
URIs: https://packages.microsoft.com/repos/code
Suites: stable
Components: main
Architectures: @@ARCHITECTURE@@
Signed-By: $CODE_TRUSTED_PART
EOF
		if [ -f "$CODE_SOURCE_PART" ]; then
			rm -f "$CODE_SOURCE_PART"
		fi

		# Sourced from https://packages.microsoft.com/keys/microsoft.asc
		if [ ! -f $CODE_TRUSTED_PART ]; then
			echo "-----BEGIN PGP PUBLIC KEY BLOCK-----
Version: BSN Pgp v1.1.0.0

mQENBFYxWIwBCADAKoZhZlJxGNGWzqV+1OG1xiQeoowKhssGAKvd+buXCGISZJwT
LXZqIcIiLP7pqdcZWtE9bSc7yBY2MalDp9Liu0KekywQ6VVX1T72NPf5Ev6x6DLV
7aVWsCzUAF+eb7DC9fPuFLEdxmOEYoPjzrQ7cCnSV4JQxAqhU4T6OjbvRazGl3ag
OeizPXmRljMtUUttHQZnRhtlzkmwIrUivbfFPD+fEoHJ1+uIdfOzZX8/oKHKLe2j
H632kvsNzJFlROVvGLYAk2WRcLu+RjjggixhwiB+Mu/A8Tf4V6b+YppS44q8EvVr
M+QvY7LNSOffSO6Slsy9oisGTdfE39nC7pVRABEBAAG0N01pY3Jvc29mdCAoUmVs
ZWFzZSBzaWduaW5nKSA8Z3Bnc2VjdXJpdHlAbWljcm9zb2Z0LmNvbT6JATQEEwEI
AB4FAlYxWIwCGwMGCwkIBwMCAxUIAwMWAgECHgECF4AACgkQ6z6Urb4SKc+P9gf/
diY2900wvWEgV7iMgrtGzx79W/PbwWiOkKoD9sdzhARXWiP8Q5teL/t5TUH6TZ3B
ENboDjwr705jLLPwuEDtPI9jz4kvdT86JwwG6N8gnWM8Ldi56SdJEtXrzwtlB/Fe
6tyfMT1E/PrJfgALUG9MWTIJkc0GhRJoyPpGZ6YWSLGXnk4c0HltYKDFR7q4wtI8
4cBu4mjZHZbxIO6r8Cci+xxuJkpOTIpr4pdpQKpECM6x5SaT2gVnscbN0PE19KK9
nPsBxyK4wW0AvAhed2qldBPTipgzPhqB2gu0jSryil95bKrSmlYJd1Y1XfNHno5D
xfn5JwgySBIdWWvtOI05gw==
=zPfd
-----END PGP PUBLIC KEY BLOCK-----
" | gpg --dearmor > $CODE_TRUSTED_PART
			if [ -f "$CODE_TRUSTED_PART_OLD" ]; then
				rm -f "$CODE_TRUSTED_PART_OLD"
			fi
		fi
	fi
fi
